package com.IClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.IClass.App.R;
import com.IClass.Server.AppJsonDefine.GenJavaCode.homeblogcomment;
import com.IClass.iHotBlogListAdapter.ListItemView;
import com.IClass.image.AsynImageLoader;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class IBlogCommentListAdapter extends BaseExpandableListAdapter 
{
	public static String TAG = "iHotBlogListAdapter";
	private AsynImageLoader mImageAsynLoader;
	private Context context;                        //运行上下文   
	private List<homeblogcomment> commentlist;    //商品信息集合   
	private LayoutInflater listContainer;           //视图容器   
    private static SimpleDateFormat commenttimeformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	public final class CommentItemView
	{                //自定义控件集合     
         public ImageView userimage;     
         public TextView username;     
         public TextView commenttime;   
         public TextView commenttext;
      
	}   
	public final class CommentGroupHeader
	{                //自定义控件集合
		public ImageView expandimage;
        public TextView grouptext;
        public Button commentbutton;
      
	}   
	  
	public IBlogCommentListAdapter(Context context, List<homeblogcomment> commentslist) 
	{   
	        this.context = context;            
	        listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文   
	        this.commentlist = commentslist;   
	        mImageAsynLoader = new AsynImageLoader();
	} 
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		
		return this.commentlist.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		homeblogcomment comment= (homeblogcomment)this.getChild(0,childPosition);//存放图片所对应的url
		
		CommentItemView  commentview = null;
		if (convertView == null) {   
			commentview = new CommentItemView();    
            //获取list_item布局文件的视图   
            convertView = listContainer.inflate(R.layout.iask_blogcommentitem, null);   
            //获取控件对象   

//            map.put("username", m.getValue().username);
//	        map.put("body_data", m.getValue().body_data);
//	        map.put("body_template", m.getValue().body_template);
//	        map.put("image_1", m.getValue().image_1);
//	        map.put("title_data", m.getValue().title_data);
//	        map.put("title_template", m.getValue().title_template);
//	        map.put("userimage", IClassApi.SERVER_HOST + "uc_server/"+ m.getValue().avatarurl);
	        
            commentview.userimage = (ImageView)convertView.findViewById(R.id.userimage);
            commentview.username = (TextView)convertView.findViewById(R.id.commentuername);
            commentview.commenttime = (TextView)convertView.findViewById(R.id.commenttime);
            commentview.commenttext = (TextView)convertView.findViewById(R.id.commenttext);
            
            convertView.setTag(commentview);
            
            commentview.userimage.setTag( IClassApi.SERVER_HOST + "uc_server/"+ comment.avatarurl);
            
		}
		else
		{
			commentview = (CommentItemView)convertView.getTag();   
		}	
		
		commentview.username.setText(comment.author);
		
		commentview.commenttime.setText(commenttimeformat.format(new Date(Long.parseLong(comment.dateline) * 1000)));
		commentview.commenttext.setText(comment.message);
		
		
		
		mImageAsynLoader.loadBitmap(commentview.userimage,ImageUtil.getBitmapById(convertView.getContext(), R.drawable.icon_3_n));
		
		
		
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.commentlist.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
                ViewGroup.LayoutParams.FILL_PARENT, 64);  

        TextView text = new TextView(context);  
        text.setLayoutParams(lp);  
        // Center the text vertically  
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        // Set the text starting position  
        text.setPadding(36, 0, 0, 0);  
          
        text.setText("评论:" + getChildrenCount(0));  
        
        return text;
        
		// TODO Auto-generated method stub
		//return getGenericView("评论:" + getChildrenCount(0));
//		CommentGroupHeader groupview = null;  
//        if (convertView == null) 
//        {  
//        	groupview = new CommentGroupHeader();    
//            //获取list_item布局文件的视图   
//            convertView = listContainer.inflate(R.layout.iask_blogcommentitemhead, null);   
//            
//            groupview.grouptext = (TextView)convertView.findViewById(R.id.expand);
//            groupview.commentbutton = (Button)convertView.findViewById(R.id.commentbutton);
//            groupview.expandimage = (ImageView)convertView.findViewById(R.id.commentexpand);
//            convertView.setTag(groupview);
//            
//        }
//        else 
//        {  
//        	groupview = (CommentGroupHeader) convertView.getTag();  
//        }  
//  
//       
//        groupview.grouptext.setText("评论:" + getChildrenCount(0));
//        
//        if(isExpanded)
//        	groupview.expandimage.setBackgroundResource(R.drawable.expandopen);
//        else
//        	groupview.expandimage.setBackgroundResource(R.drawable.expandclose);
//        // Set the text starting position  
//        
//        return convertView;
        //return getGenericView("评论:" + getChildrenCount(0)); 
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	//View stub to create Group/Children 's View  
    public TextView getGenericView(String s) {  
        // Layout parameters for the ExpandableListView  
    	AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
                ViewGroup.LayoutParams.FILL_PARENT, 64);  

        TextView text = new TextView(context);  
        text.setLayoutParams(lp);  
        // Center the text vertically  
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        // Set the text starting position  
        text.setPadding(36, 0, 0, 0);  
          
        text.setText(s);  
        return text; 
    }  


}
